<template>
  <div class="p-2 container">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="路线名称" prop="patrolRouteName">
              <el-input v-model="queryParams.patrolRouteName" placeholder="请输入路线名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="路线描述" prop="patrolRouteDesc">
              <el-input v-model="queryParams.patrolRouteDesc" placeholder="请输入路线描述" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="创建时间" prop="">
              <el-date-picker
                v-model="startAndEnd"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD"
                @change="handleDateChange"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never" style="flex: 1; display: flex; flex-direction: column; position: relative;">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:patrolRoute:add']" type="primary" plain icon="Plus" @click="handleAdd">
              新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:patrolRoute:edit']" type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()">
              修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:patrolRoute:remove']" type="danger" plainicon="Delete" :disabled="multiple" @click="handleDelete()">
              删除
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="patrolrouteList" style="flex: 1;" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="主键" align="center" prop="cameraPatrolRouteId" />
        <el-table-column label="路线名称" align="center" prop="patrolRouteName" show-overflow-tooltip />
        <el-table-column label="路线描述" align="center" prop="patrolRouteDesc" show-overflow-tooltip />
        <el-table-column label="调用摄像头" align="center" prop="cameraNameS" min-width="100px" show-overflow-tooltip />
        <el-table-column label="累计使用次数" align="center" prop="accumulateNum" min-width="120px" show-overflow-tooltip />
        <el-table-column label="创建人" align="center" prop="createName" show-overflow-tooltip />
        <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" min-width="150px">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['camera:patrolRoute:edit']" link type="primary" @click="handleUpdate(scope.row)">
                修改
              </el-button>
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <el-button v-hasPermi="['camera:patrolRoute:query']" link type="primary" @click="handleDetail(scope.row)">
                详情
              </el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['camera:patrolRoute:remove']" linktype="primary" @click="handleDelete(scope.row)">
                删除
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <!-- 添加巡视路线对话框 -->
    <el-drawer v-model="dialog.shouAdd" :title="dialog.title" append-to-body destroy-on-close size="900px">
      <add @close="dialog.shouAdd = false" @success="getList" />
    </el-drawer>

    <!-- 修改巡视路线对话框 -->
    <el-drawer v-model="dialog.showUpdate" :title="dialog.title" append-to-body destroy-on-close  size="900px">
      <update :camera-patrol-route-id="cameraPatrolRouteId" @close="dialog.showUpdate = false" @success="getList" />
    </el-drawer>

    <!-- 巡视路线详情对话框 -->
    <el-drawer v-model="dialogDetail.shouAdd" :title="dialogDetail.title" width="500px" append-to-body destroy-on-close size="45%">
      <detail :camera-patrol-route-id= "cameraPatrolRouteId" @close="dialogDetail.shouAdd = false" @success="getList"  />
    </el-drawer>
  </div>
</template>

<script setup name="patrolroute" lang="ts">
import { listpatrolroute, delpatrolroute } from '@/api/security/video-patrol/camera-patrol-route';
import { patrolrouteVO, patrolrouteQuery, patrolrouteForm } from '@/api/security/video-patrol/camera-patrol-route/types';
import add from './components/add.vue';
import update from './components/update.vue';
import detail from './components/detail.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const patrolrouteList = ref<patrolrouteVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const dialogDetail = reactive<DialogOption>({
  visible: false,
  title: '视频巡检路线详情'
});

const initFormData: patrolrouteForm = {
  cameraPatrolRouteId: undefined,
  patrolRouteName: undefined,
  patrolRouteDesc: undefined,
  cameraIdS: undefined
};
const data = reactive<PageData<patrolrouteForm, patrolrouteQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    patrolRouteName: undefined,
    patrolRouteDesc: undefined,
    params: {}
  },
  rules: { }
});

const { queryParams } = toRefs(data);

/** 查询巡视路线列表 */
const getList = async () => {
  loading.value = true;
  const res = await listpatrolroute(queryParams.value);
  patrolrouteList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  startAndEnd.value = [];
  queryParams.value.startTime = undefined;
  queryParams.value.endTime = undefined;
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: patrolrouteVO[]) => {
  ids.value = selection.map(item => item.cameraPatrolRouteId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  dialog.shouAdd = true;
  dialog.title = '添加巡视路线';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: patrolrouteVO) => {
  cameraPatrolRouteId.value = row?.cameraPatrolRouteId || ids.value[0];
  dialog.showUpdate = true;
  dialog.title = '修改巡视路线';
};

/** 删除按钮操作 */
const handleDelete = async (row?: patrolrouteVO) => {
  const _cameraPatrolRouteIds = row?.cameraPatrolRouteId || ids.value;
  await proxy?.$modal.confirm('是否确认删除巡视路线编号为"' + _cameraPatrolRouteIds + '"的数据项？').finally(() => loading.value = false);
  await delpatrolroute(_cameraPatrolRouteIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('camera/patrolRoute/export', {
    ...queryParams.value
  }, `patrolRoute_${new Date().getTime()}.xlsx`);
};

/** 日期框改变 */
let startAndEnd = ref([]);
const handleDateChange = () => {
  queryParams.value.startTime = startAndEnd.value[0]+ ' 00:00:00';
  queryParams.value.endTime = startAndEnd.value[1]+ ' 23:59:59';
};

/** 详情按钮操作 */
const cameraPatrolRouteId = ref();
const handleDetail = async (row?: patrolrouteVO) => {
  cameraPatrolRouteId.value = row?.cameraPatrolRouteId || ids.value[0];
  dialogDetail.shouAdd = true;
};

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
:deep(.el-card__header) {
  height: 60px;
}

:deep(.el-card__body) {
  height: calc(100% - 60px);
  display: flex;
  flex-direction: column;
}

:deep(.pagination-container) {
  margin-top: 0;
}

</style>
